local gamelib = require "club.threecard.gamelib"
local slotlib = require "club.threecard.slotlib"
local components = slotlib.require "components"

local slot_system = slotlib.systems.slot_system
local class = gamelib.entitas.class
local Matcher = gamelib.entitas.Matcher
local GroupEvent = gamelib.entitas.GroupEvent

local cover_symbol_system = class("cover_symbol_system", slot_system)

function cover_symbol_system:initialize()
    if self.ingame.cover_panel then
        self.ingame.cover_panel:SetActive(false);
    end
end

function cover_symbol_system:get_trigger()
    return {{Matcher({components.frames_component}), GroupEvent.ADDED | GroupEvent.UPDATE}};
end

function cover_symbol_system:filter(entity)
    return entity:has(components.frames_component)
end

function cover_symbol_system:execute(entitas)
    if entitas:size() ~= 1 then
        gamelib.log.error("数据错误，结果组件不可能~=1->"..entitas:size())
        return;
    end

    entitas:foreach(function(e)
        local frames = e:get(components.frames_component).frames;
        if e:has(components.cover_symbol_component) then
            e:remove(components.cover_symbol_component);
        end
        
        if #frames == 0 then
            return;
        end

        if not self.ingame.cover_panel then
            return;
        end
        
        e:replace(components.cover_symbol_component, self.ingame.cover_panel.transform);
        self.ingame.cover_panel:SetActive(true);
    end)
end

function cover_symbol_system:cleanup()
    if self.ingame.cover_panel then
        self.ingame.cover_panel:SetActive(false);
    end
end

function cover_symbol_system:tear_down()
    if self.ingame.cover_panel then
        self.ingame.cover_panel:SetActive(false);
    end
end

return cover_symbol_system